package mulMan.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Observable;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import mulMan.domain.AudioFileFactory;
import mulMan.domain.AudioFileModel;
import mulMan.domain.PlaylistFactory;
import mulMan.domain.PlaylistModel;
import mulMan.gui.view.AddFileView;
import mulMan.gui.view.AddPlaylistView;
import mulMan.gui.view.AudioFilesView;
import mulMan.gui.view.ModifyPlaylistView;
import mulMan.gui.view.PlaylistsView;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class PlaylistsController extends Observable implements ApplicationContextAware{
	
	private List<PlaylistModel> playlists;
	private PlaylistsView view;
	ApplicationContext applicationContext;
	

	public PlaylistsView getView() {
		return view;
	}
	public void setView(PlaylistsView view) {
		this.view = view;
	}
	
	
	public PlaylistsController(List<PlaylistModel> playlists, PlaylistsView view) {
		this.playlists = playlists;
		this.view = view;
		
		view.getListOfPlaylists().setSelectedIndex(0);
		
		//addAudioFilesView();
		view.addListPlaylistsListener(new ListPlaylistsListener());
		view.addAddNewAudioFileListener(new AddNewAudioFileListener());
		view.addAddNewPlaylistListener(new AddNewPlaylistListener());
		view.addExitListener(new ExitListener());
		view.addListPlaylistsMouseListener(new ListPlaylistsMouseListener());
		view.adddeletePlaylistListener(new DeletePlaylistListener());
	}
	
	public void addAudioFilesView() {
		PlaylistModel selectedPlaylist = playlists.get(view.getListOfPlaylists().getSelectedIndex());
		
		AudioFilesView audioFilesView = new AudioFilesView(selectedPlaylist.getAudioFilesModels());
		applicationContext.getBean("audioFilesController", selectedPlaylist.getAudioFilesModels(), audioFilesView);
		
		view.setAudioFilesView(audioFilesView);
		
	}
	
	
	//ACTION LISTENERS
	
	class ListPlaylistsMouseListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			if((e.getClickCount() == 2) && (!playlists.get(view.getListOfPlaylists().getSelectedIndex()).getName().equals("Music"))){
				
	            new PlaylistController(playlists.get(view.getListOfPlaylists().getSelectedIndex()), 
	            		new ModifyPlaylistView(playlists.get(view.getListOfPlaylists().getSelectedIndex())));
	            
			}
		}

		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showPopup(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if ((e.isPopupTrigger()) && (!playlists.get(view.getListOfPlaylists().locationToIndex(e.getPoint())).getName().equals("Music"))) {
				showPopup(e);
			}
		}

		private void showPopup(MouseEvent e) {
			view.getListOfPlaylists().setSelectedIndex(view.getListOfPlaylists().locationToIndex(e.getPoint()));
			view.getPopupMenu().show(view.getListOfPlaylists(), e.getX(), e.getY());
		}
    }
	
	
	class AddNewAudioFileListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			PlaylistModel selectedPlaylist = playlists.get(view.getListOfPlaylists().getSelectedIndex());
			
			AudioFileFactory audioFileFacory = (AudioFileFactory) applicationContext.getBean("audioFileFactory");
			AudioFileModel audioFileModel = audioFileFacory.addAudioFileToPlaylist(selectedPlaylist);
			
			new AudioFileController(audioFileModel, new AddFileView(audioFileModel));
		}
		
	}
	
	
	class DeletePlaylistListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			playlists.remove(view.getListOfPlaylists().getSelectedIndex());
			view.getListOfPlaylists().setSelectedIndex(0);
			addAudioFilesView();
		}
		
	}
	
	class AddNewPlaylistListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			PlaylistFactory playlistFacory = (PlaylistFactory) applicationContext.getBean("playlistFactory");
			PlaylistModel playlistModel = playlistFacory.addPlaylistToListOfPlaylist(playlists);
			
			new PlaylistController(playlistModel, new AddPlaylistView(playlistModel));
		}
		
	}
	
	class ExitListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
		
	}
	
	class ListPlaylistsListener implements ListSelectionListener  {

		public void valueChanged(ListSelectionEvent arg0) {
			if (arg0.getValueIsAdjusting()) {
	            addAudioFilesView();
	        }
		}

    }
	
	
	
	
	
	
	

	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.applicationContext = arg0;
	}
	
	
}
